package com.sense.kukoo.biz_messages.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_messages.model.MessagesCommentsModel
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.biz.MessagesItemBean
import com.sense.kukoo.common.repo.http.bean.UserMessageCommentsBean

class MessagesCommentsViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    private var mIndex = 2
    private val mPageSize = 10
    private var mNewsNum = 0
    private var mOnlyShowNews = true
    private var mEnableLoadMore = true
    private var mNetworkRetry: ()->Unit = {}
    private val mComments: ArrayList<UserMessageCommentsBean> = arrayListOf()

    val action_back: MutableLiveData<Boolean> = MutableLiveData()
    val action_networkError: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishRefresh: MutableLiveData<Boolean> = MutableLiveData()
    val data_comments:MutableLiveData<ArrayList<MessagesItemBean>> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    fun setNewsNum(newsNum: Int){
        mNewsNum = newsNum
        if(newsNum <= 0){
            mOnlyShowNews = false
        } else if(newsNum > mPageSize){
            mOnlyShowNews = false
        }
    }

    override fun initData() {}

    fun onBackClick(){
        action_back.postValue(true)
    }

    private fun showCommentsData(){
        val list = arrayListOf<MessagesItemBean>()
        if (mComments.isEmpty()) {
            list.add(MessagesItemBean(MessagesItemBean.TYPE_EMPTY_LIST, emptyType = MessagesItemBean.TYPE_COMMENTS))
        } else {
            mComments.forEach {
                list.add(MessagesItemBean(MessagesItemBean.TYPE_COMMENTS, comment = it))
            }
        }
        if (mEnableLoadMore) {
            list.add(MessagesItemBean(MessagesItemBean.TYPE_VIEW_MORE))
        }
        data_comments.postValue(list)
        action_finishRefresh.postValue(true)
    }

    fun refreshCommentsData(){
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mNetworkRetry = {refreshCommentsData()}
            action_networkError.postValue(true)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            var pageSize = mPageSize
            if (mOnlyShowNews) {
                pageSize = mNewsNum
            }
            val result = Repo.http.messageComments(1, pageSize)
            if (result.isSuccess()) {
                mIndex = 2
                result.data?.let {
                    mComments.clear()
                    mComments.addAll(it)
                    if (mOnlyShowNews) {
                        mEnableLoadMore = true
                    } else {
                        if(it.size < mPageSize){
                            mEnableLoadMore = false
                        } else {
                            mEnableLoadMore = true
                        }
                    }
                    showCommentsData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mNetworkRetry = {refreshCommentsData()}
                action_networkError.postValue(true)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun loadMoreCommentsData(){
        if (mOnlyShowNews) {
            mOnlyShowNews = false
            refreshCommentsData()
            return
        }
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mNetworkRetry = {loadMoreCommentsData()}
            action_networkError.postValue(true)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            val result = Repo.http.messageComments(mIndex, mPageSize)
            if (result.isSuccess()) {
                mIndex++
                result.data?.let {
                    mComments.addAll(it)
                    if(it.size < mPageSize){
                        mEnableLoadMore = false
                    }
                    showCommentsData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mNetworkRetry = {loadMoreCommentsData()}
                action_networkError.postValue(true)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun deleteComment(comment: UserMessageCommentsBean){
        request {
            val result = Repo.http.messageDelete(comment.id, 3)
            if (result.isSuccess() && true == result.data) {
                mComments.remove(comment)
                showCommentsData()
            } else {
                Log.e(TAG, "result=" + result.msg)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun onNetworkRetry(){
        mNetworkRetry()
    }
}